//
//  LoadingView.m
//  TianyaQing
//
//  Created by Liccon Chang on 12-4-16.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "LoadingView.h"
#import <QuartzCore/QuartzCore.h>

@interface LoadingView(Private)
- (void)rotating;
@end

#define SMALL_STYLE_WIDTH 60
#define LARGE_STYLE_WIDTH 90
#define LARGE_TEXT_STYLE_WIDTH 120

@implementation LoadingView

- (id)initWithParentSize:(CGSize)size withText:(NSString*)text withStyle:(LoadingType)type
{
    CGRect frame=CGRectZero;
    if (type==ESmallStyle)
    {
        frame=CGRectMake(0, 0, SMALL_STYLE_WIDTH, SMALL_STYLE_WIDTH);
    }
    else
    {
        frame=CGRectMake(0, 0, LARGE_STYLE_WIDTH, LARGE_STYLE_WIDTH);
    }
    self = [self initWithFrame:frame];
    {
        if (type==ESmallStyle)
        {
            frame=CGRectMake((size.width -LARGE_STYLE_WIDTH)/2, (size.height -LARGE_STYLE_WIDTH)/2, LARGE_STYLE_WIDTH, LARGE_STYLE_WIDTH);
        }
        else
        {
            frame=CGRectMake((size.width -LARGE_TEXT_STYLE_WIDTH)/2, (size.height -LARGE_TEXT_STYLE_WIDTH)/2,LARGE_TEXT_STYLE_WIDTH, LARGE_TEXT_STYLE_WIDTH);
        }
        self.frame=frame;
        if ([text length]>0)
        {
            loadingLabel_=[[UILabel alloc] initWithFrame:CGRectMake(0, frame.size.height-25, frame.size.width, 20)];
            loadingLabel_.textAlignment=UITextAlignmentCenter;
            loadingLabel_.backgroundColor=[UIColor clearColor];
            loadingLabel_.textColor=[UIColor whiteColor];
            loadingLabel_.font=[UIFont systemFontOfSize:12];
            loadingLabel_.text=text;
            [self addSubview:loadingLabel_];
            
            self.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.5];
            self.layer.cornerRadius=5.0;
        }
        frame=rotateBackView_.frame;
        frame.origin.x=(self.frame.size.width-frame.size.width)/2;
        frame.origin.y=(self.frame.size.height-frame.size.height)/2-10;
        rotateBackView_.frame=frame;
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
	{
        curPercent_=0.0f;
        //self.image=[UIImage imageNamed:@"earth.png"];
        CGRect rect=frame;
        rect.origin=CGPointMake(0, 0);
        
        rotateBackView_=[[UIImageView alloc] initWithFrame:rect];
        rotateBackView_.image=[UIImage imageNamed:@"earth.png"];
        
        
        rotateView_=[[UIImageView alloc] initWithFrame:rect];
        rotateView_.image=[UIImage imageNamed:@"earth_blue_line.png"];
        [rotateBackView_ addSubview:rotateView_];
        
        rotateView1_=[[UIImageView alloc] initWithFrame:rect];
        rotateView1_.image=[UIImage imageNamed:@"earth_blue_line.png"];
        [rotateBackView_ addSubview:rotateView1_];
        
        rotateView2_=[[UIImageView alloc] initWithFrame:rect];
        rotateView2_.image=[UIImage imageNamed:@"earth_blue_line.png"];
        rotateView2_.layer.anchorPoint = CGPointMake(0.5, 0.5);  
        rotateView2_.transform = CGAffineTransformMakeRotation(M_PI*0.5);
        [rotateBackView_ addSubview:rotateView2_];
        
        rotateView3_=[[UIImageView alloc] initWithFrame:rect];
        rotateView3_.image=[UIImage imageNamed:@"earth_blue_line.png"];
        rotateView3_.layer.anchorPoint = CGPointMake(0.5, 0.5);  
        rotateView3_.transform = CGAffineTransformMakeRotation(M_PI);
        [rotateBackView_ addSubview:rotateView3_];
        
        rotateView4_=[[UIImageView alloc] initWithFrame:rect];
        rotateView4_.image=[UIImage imageNamed:@"earth_blue_line.png"];
        rotateView4_.layer.anchorPoint = CGPointMake(0.5, 0.5);  
        rotateView4_.transform = CGAffineTransformMakeRotation(M_PI*3/2);
        [rotateBackView_ addSubview:rotateView4_];
        
        [self addSubview:rotateBackView_];
        [rotateBackView_ release];
    }
    return self;
}

//填充外围
- (void)fillingOutsize:(CGFloat)percent
{
    rotateView1_.hidden=FALSE;
    if (fabsf(percent)>=0.25) 
    {
        rotateView2_.hidden=FALSE;
    }
    else
    {
        rotateView2_.hidden=TRUE;
    }
    if (fabsf(percent)>=0.5)
    {
        rotateView3_.hidden=FALSE;
    }
    else
    {
        rotateView3_.hidden=TRUE;
    }
    if (fabsf(percent)>=0.75)
    {
        rotateView4_.hidden=FALSE;
    }
    else
    {
        rotateView4_.hidden=TRUE;
    }
    [UIView beginAnimations:nil context:nil];  
    [UIView setAnimationDuration:1.0f];  
    [UIView setAnimationRepeatAutoreverses:YES];  
    [UIView setAnimationRepeatCount:10];  
    rotateView_.layer.anchorPoint = CGPointMake(0.5, 0.5);  
    rotateView_.transform = CGAffineTransformMakeRotation(2*M_PI*percent);
    [UIView commitAnimations];
}

//让外围开始的旋转
- (void)startRotating
{
    rotateView1_.hidden=TRUE;
    rotateView2_.hidden=TRUE;
    rotateView3_.hidden=TRUE;
    rotateView4_.hidden=TRUE;
    timer_=[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(rotating) userInfo:nil repeats:YES];
}

- (void)rotating
{
    [UIView beginAnimations:nil context:nil];  
    [UIView setAnimationDuration:1.0f];  
    [UIView setAnimationRepeatAutoreverses:YES];  
    [UIView setAnimationRepeatCount:10];  
    rotateView_.layer.anchorPoint = CGPointMake(0.5, 0.5);  
    rotateView_.transform = CGAffineTransformMakeRotation(2*M_PI*curPercent_);
    [UIView commitAnimations];
    curPercent_=curPercent_+0.02;
}

//让外围停止的旋转
- (void)stopRotating
{
    [timer_ invalidate];
    timer_=nil;
    [rotateView_.layer removeAllAnimations];
}

- (void)setLoadingText:(NSString*)text
{
    if (loadingLabel_==nil)
    {
        CGRect rect=self.frame;
        loadingLabel_=[[UILabel alloc] initWithFrame:CGRectMake(15, rect.size.height+5, rect.size.width, 20)];
        loadingLabel_.textAlignment=UITextAlignmentCenter;
        loadingLabel_.backgroundColor=[UIColor clearColor];
        loadingLabel_.textColor=[UIColor whiteColor];
        loadingLabel_.font=[UIFont systemFontOfSize:12];
        [self addSubview:loadingLabel_];
        rect.size.width+=30;
        rect.size.height+=30;
        self.frame=rect;
        rect=rotateBackView_.frame;
        rect.origin.x+=15;
        rect.origin.y+=10;
        rotateBackView_.frame=rect;
        
        self.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.5];
        self.layer.cornerRadius=5.0;
        
    }
    loadingLabel_.text=text;
}

- (void)dealloc
{
    [timer_ invalidate];
    timer_=nil;
    [rotateView_ release];
    [rotateView1_ release];
    [rotateView2_ release];
    [rotateView3_ release];
    [rotateView4_ release];
    [loadingLabel_ release];
    [super dealloc];
}

@end
